#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

#define FILENAME_SIZE 70

int main(int argc, char** argv) {
	if (argc == 3) {
		int bufSize = (atoi(argv[1]) * atoi(argv[2]));
		FILE *pFile1 = NULL;
		char *filename1 = (char*) malloc(FILENAME_SIZE);
		char *filename2 = (char*) malloc(FILENAME_SIZE);
		char *filename3 = (char*) malloc(FILENAME_SIZE);
		memset(filename1, '\0', FILENAME_SIZE);
		char *buffer = (char*) malloc(bufSize);
		char *buffer2 = (char*) malloc(500);
		memset(buffer, '\0', bufSize);
		memset(buffer2, '\0', 500);
		sprintf(filename1, "/home/utnso/Escritorio/Archivos de Prueba/%dbloques", atoi(argv[1]));
		sprintf(filename2, "/home/utnso/prueba%dbloques", atoi(argv[1]));
		sprintf(filename3, "/home/utnso/Escritorio/FUSE/%dbloques", atoi(argv[1]));

		sprintf(buffer2, "cp \"%s\" \"%s\" && cp \"%s\" \"%sRES\" && md5sum \"%s\" && md5sum \"%sRES\"", filename1, filename3, filename3, filename1, filename1, filename1);

		int i;
		int caracter = 0;
		for (i = 0; i < bufSize; i++) {
			if (caracter == 0) {
				buffer[i] = '0';
				caracter++;
			} else if (caracter == 1) {
				buffer[i] = '1';
				caracter++;
			} else if (caracter == 2) {
				buffer[i] = '2';
				caracter++;
			} else if (caracter == 3) {
				buffer[i] = '3';
				caracter++;
			} else if (caracter == 4) {
				buffer[i] = '4';
				caracter++;
			} else if (caracter == 5) {
				buffer[i] = '5';
				caracter++;
			} else if (caracter == 6) {
				buffer[i] = '6';
				caracter++;
			} else if (caracter == 7) {
				buffer[i] = '7';
				caracter++;
			} else if (caracter == 8) {
				buffer[i] = '8';
				caracter++;
			} else if (caracter == 9) {
				buffer[i] = '9';
				caracter = 0;
			}
		}

		pFile1 = fopen(filename1, "w");
		int fd = open(filename2, O_RDWR | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
		if (pFile1 == NULL) {
			printf("No se pudo abrir el archivo 1\n");
		}
		if (fd == -1) {
			printf("No se pudo abrir el archivo 2\n");
		}

		fwrite(buffer, bufSize, 1, pFile1);
		write(fd, buffer2, strlen(buffer2));

		printf("Listópolis\n");
		close(fd);
	}
	return 0;
}
